/**
 * Comparators.java
 * @author: liuex
 * 2007-12-24 ����10:08:49
 *
 * note: 
 */
package org.lex.utils;

import java.text.Collator;
import java.util.Comparator;

public class Comparators {
	private static Collator collator = Collator.getInstance();
	private static Comparator<Object> stringComparatpor = new Comparator<Object>() {
		public int compare(Object o1, Object o2) {
			if (null == o1)
				return -1;
			if (null == o2)
				return 1;
			return collator.compare(o1, o2);
		}
	};

	public static Comparator<Object> stringComparator() {
		return stringComparatpor;
	}

	public static <T extends Comparable<T>> Comparator<T> newComparator(T c) {
		return new Comparator<T>() {
			public int compare(T o1, T o2) {
				return o1.compareTo(o2);
			}
		};
	}

	public static <T> Comparator<T> newComparator(final CategoryComparator<T> cc) {
		return new Comparator<T>() {
			public int compare(T a, T b) {
				int cata = cc.getCategory(a);
				int catb = cc.getCategory(b);
				if (cata != catb)
					return cata - catb;
				else
					return cc.compare(a, b);
			}
		};
	}
}
